package com.budgetme.ui.activities;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;

import com.budgetme.R;
import com.budgetme.database.Database;
import com.budgetme.widgets.BudgetWidget;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class AddExpenseSimple extends AbstractActivity {
	
	private static final String TAG = "AddExpenseSimple";
	
	public static final String ARG_BUDGET_ID = "ARG_BUDGET_ID";
	public static final String ARG_WIDGET_ID = "ARG_WIDGET_ID";
	
	private int mBudgetId;
	private int mAppWidgetId;
	
	private EditText mExpenseInput;
	private Button mExpenseSubmit;
	
	private ContentResolver mContentResolver;
	
	// TODO FEATURE: Add double 0 to input keyboard
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
		
		setContentView(R.layout.add_expense);
		
		mContentResolver = getContentResolver();
		
		Bundle extras = getIntent().getExtras();
		mBudgetId = extras.getInt(ARG_BUDGET_ID);
		mAppWidgetId = extras.getInt(ARG_WIDGET_ID);
		
		this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		
		mExpenseInput = (EditText) findViewById(R.id.expense_amount_input);
		mExpenseInput.addTextChangedListener(new TextWatcher() {
			
			// TODO FEATURE: Add handling for adding/deleting numbers in the middle 
			// of the edittext.  Added ontouch to force
			@Override
			public void afterTextChanged(Editable s) {
				
				mExpenseInput.removeTextChangedListener(this);
				
				String cleanString = mExpenseInput.getText().toString().replaceAll("[$,.]", "");
				double cur = Double.valueOf(cleanString.toString()) / 100;
				NumberFormat nf = NumberFormat.getCurrencyInstance();
				String formattedString = nf.format(cur);
				mExpenseInput.setText(formattedString);
				mExpenseInput.setSelection(formattedString.length());
				
				mExpenseInput.addTextChangedListener(this);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			
		});
		
		// This forces the cursor to the end of the edittext.  To protect against
		// adding or deleting in the middle.
		mExpenseInput.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				mExpenseInput.setSelection(mExpenseInput.getText().toString().length());
				return true;
			}
		});
		
		mExpenseInput.setText("$0.00");
		mExpenseInput.setSelection("$0.00".length());
		
		
		
		mExpenseSubmit = (Button) findViewById(R.id.expense_submit);
		mExpenseSubmit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
				String date = format.format(new Date());
				
				
				String cleanString = mExpenseInput.getText().toString().replaceAll("[$,.]", "");
				double amount = Double.valueOf(cleanString.toString()) / 100;
				
				ContentValues values = new ContentValues();
				values.put(Database.tbl_expenses.b_id, mBudgetId);
				values.put(Database.tbl_expenses.e_amount, amount);
				values.put(Database.tbl_expenses.e_date, date);
				
				mContentResolver.insert(Database.tbl_expenses.CONTENT_URI, values);
				
				AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(AddExpenseSimple.this);
			    BudgetWidget.updateBudgetWidget(AddExpenseSimple.this, appWidgetManager, mAppWidgetId);
			      
			    finish();
				
			}
			
		});
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
	}


}
